<?php
class FlashController extends App_Zend_Controller_Action_Shop
{
	public function indexAction()
	{
		$this->view->styleName = 'blackfriday';
    	$this->view->webSiteTitle = 'GAGA Soccer Jersey DEALS, 80% OFF for GAGA Deals';
    	$this->view->webSiteDescription = 'Shop for Soccer Jerseys through bestcheapjerseydeal GAGA Deals';
    	$this->view->webSiteKeywords = 'GAGA Soccer Jersey DEALS, GAGA Soccer Jersey, GAGA Deals';

    	$flashSale = new CFlashsales();
		$currentTime = $flashSale->getCurrentBeginTime();
		$time = strtotime(date('Y-m-d H:i', time()));
		if ($currentTime < $time) {
			file_get_contents(MY_URL . '/cron-flashsale');
		}
    	
		$items = $flashSale->getSaleItem();
		$this->view->items = $items;
		
		$endTime = $flashSale->getTime('end');
		$leftTime = 0;
		if($endTime == -1){
			$this->view->timeType = 'start';
			$leftTime = $flashSale->getTime('start')-time();
		}else{
			$this->view->timeType = 'end';
			$leftTime = $endTime-time();
		}
		$hours = (int)($leftTime/3600);
		if($hours<10){
			$hours ='0'.$hours;
		}
		$minutes = (int)(($leftTime-((int)($leftTime/3600))*3600)/60);
		if($minutes<10){
			$minutes ='0'.$minutes;
		}
		$seconds = $leftTime%60;
		if($seconds<10){
			$seconds ='0'.$seconds;
		}
		$this->view->hours = $hours;
		$this->view->minutes = $minutes;
		$this->view->seconds = $seconds;
		$this->view->curtime = $date = date('Y-m-d H:i', time());
		$this->view->noGagaDeal = 1;
		
		//$this->view->mlmAccount = 20;
		//$this->view->gagaItemCount = 0;
		$flashCommon = new CFlashcommon();
        $uid = isset($_COOKIE[COOKIE_PRE . 'uid']) ? $_COOKIE[COOKIE_PRE . 'uid']:0;
        $mlmAccount = $flashCommon->getAvaiAmount($uid);
        $this->view->mlmAccount = $mlmAccount;
        $this->view->gagaItemCount = $flashCommon->getAvaiItemCount($uid);
        
        $this->view->curFlashItem = $flashCommon->getCurFlashItem($uid);
        $whenEverItems = $flashCommon->getCurFlashWhenEverItem($uid);
       
        $this->view->whenEverItems = $whenEverItems;
	}
	
	public function frameAction()
	{
		$this->getHelper('layout')->disableLayout();

    	$flashSale = new CFlashsales();
		$currentTime = $flashSale->getCurrentBeginTime();
		$time = strtotime(date('Y-m-d H:i', time()));
		
		if ($currentTime < $time) {
			file_get_contents(MY_URL . '/cron-flashsale');
		}
    	
		
		$items = $flashSale->getSaleItem();
		$this->view->items = $items;
		
		$endTime = $flashSale->getTime('end');
		$leftTime = 0;
		if($endTime == -1){
			$this->view->timeType = 'start';
			$leftTime = $flashSale->getTime('start')-time();
		}else{
			$this->view->timeType = 'end';
			$leftTime = $endTime-time();
		}
		$hours = (int)($leftTime/3600);
		if($hours<10){
			$hours ='0'.$hours;
		}
		$minutes = (int)(($leftTime-((int)($leftTime/3600))*3600)/60);
		if($minutes<10){
			$minutes ='0'.$minutes;
		}
		$seconds = $leftTime%60;
		if($seconds<10){
			$seconds ='0'.$seconds;
		}
		$this->view->hours = $hours;
		$this->view->minutes = $minutes;
		$this->view->seconds = $seconds;
		$this->view->curtime = $date = date('Y-m-d H:i', time());
	}
	
	public function leftframeAction()
	{
		$this->getHelper('layout')->disableLayout();

    	$flashSale = new CFlashsales();
		$currentTime = $flashSale->getCurrentBeginTime();
		$time = strtotime(date('Y-m-d H:i', time()));
		
		if ($currentTime < $time) {
			file_get_contents(MY_URL . '/cron-flashsale');
		}
    	
		$items = $flashSale->getSaleItem();
		$this->view->items = $items;
		
		$endTime = $flashSale->getTime('end');
		$leftTime = 0;
		if($endTime == -1){
			$this->view->timeType = 'start';
			$leftTime = $flashSale->getTime('start')-time();
		}else{
			$this->view->timeType = 'end';
			$leftTime = $endTime-time();
		}
		$this->view->leftTime = $leftTime;
	}
	
	//支付itemCount
	public function payforitemAction(){
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		$itemCount=$this->_request->getParam('itemCount');	
		
		$flashCommon = new CFlashcommon();
		$uid = isset($_COOKIE[COOKIE_PRE . 'uid']) ? $_COOKIE[COOKIE_PRE . 'uid'] : 0;
		if ($flashCommon->buyItem($uid, $itemCount)) {
			echo $itemCount;
		} else {		
			echo 0;
		}
	}
	
	//扣减用户itemCount，并返回新加入闪购的产品
	public function subtractitemAction(){
		$this->getHelper('layout')->disableLayout();	
		$flashCommon = new CFlashcommon();
		$uid = isset($_COOKIE[COOKIE_PRE . 'uid']) ? $_COOKIE[COOKIE_PRE . 'uid'] : 0;
		if ($item = $flashCommon->getItem($uid)) {
			$flashSaleCommon = new CFlashsales();
        	$discount = $flashSaleCommon->getDiscount($item[0]['itemid']);
        	$item[0]['max_price'] = round($item[0]['max_price'] * $discount,2);
			$this->view->item = $item[0];
			
		} else {
			echo 0;
		}		
	}
	
	public function showorderAction(){
		$this->getHelper('layout')->disableLayout();	
		$uid = isset($_COOKIE[COOKIE_PRE . 'uid']) ? $_COOKIE[COOKIE_PRE . 'uid']:0;
		$flashCommon = new CFlashcommon();
        $mlmAccount = $flashCommon->getAvaiAmount($uid);
        $this->view->mlmAccount = $mlmAccount;	
	}
	
	public function wheneveritemsAction(){
		$this->getHelper('layout')->disableLayout();
		$uid = isset($_COOKIE[COOKIE_PRE . 'uid']) ? $_COOKIE[COOKIE_PRE . 'uid']:0;
		$items = array();
		$this->view->items = $items;
		$wheneverItemCount = 0;
		$flashCommon = new CFlashcommon();
		$wheneverItemCount = $flashCommon->getAvaiWhenEverItemCount($uid);
		$this->view->wheneverItemCount = $wheneverItemCount;
		$items = $flashCommon->getCurFlashWhenEverItem($uid);
		$return = array();
		$flashWhenEverItem = new CFlashwheneveritem();
		foreach ($items as $v) {
			$row = $flashWhenEverItem->fetchRow(array('uid = ?'=>$uid, 'statu = ?'=>1, 'itemid = ?'=>$v['itemid']));
			$v['endTime'] = $row->expiretime;
			$return[] = $v;
		}		
		$this->view->items = $return;
	}
	
	public function showwheneverorderAction(){
		$this->getHelper('layout')->disableLayout();	
		$uid = isset($_COOKIE[COOKIE_PRE . 'uid']) ? $_COOKIE[COOKIE_PRE . 'uid']:0;
		$flashCommon = new CFlashcommon();
        $mlmAccount = $flashCommon->getAvaiAmount($uid);
        $this->view->mlmAccount = $mlmAccount;	
	}
	
	public function payforwheneveritemAction(){
		$this->getHelper('layout')->disableLayout();
		$this->getHelper('viewRenderer')->setNoRender();
		
		$itemCount=$this->_request->getParam('itemCount');	
		
		$flashCommon = new CFlashcommon();
		$uid = isset($_COOKIE[COOKIE_PRE . 'uid']) ? $_COOKIE[COOKIE_PRE . 'uid'] : 0;
		if ($flashCommon->buyWhenEverItem($uid, $itemCount)) {
			echo $itemCount;
		} else {		
			echo 0;
		}
	}
	
	public function subtractwheneveritemAction(){
		$this->getHelper('layout')->disableLayout();	
		$flashCommon = new CFlashcommon();
		$uid = isset($_COOKIE[COOKIE_PRE . 'uid']) ? $_COOKIE[COOKIE_PRE . 'uid'] : 0;
		if ($item = $flashCommon->getWhenEverItem($uid)) {
			$flashSaleCommon = new CFlashsales();
        	$discount = $flashSaleCommon->getDiscount($item[0]['itemid']);
        	$item[0]['max_price'] = round($item[0]['max_price'] * $discount,2);
			$this->view->item = $item[0];			
		} else {
			echo 0;
		}		
		$this->view->endTime = time()+3600;
	}
	
}